package persentation.presentStudent;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;

import vo.chooseCourseVO.ChooseCourseVO;
import businesslogic.chooseCourseBl.ChooseCourse;
import businesslogic.chooseCourseBl.ChooseCourseController;
import businesslogic.userBl.StudentController;

public class ShowScorePanel extends JPanel {

	private ChooseCourseController chooseCourseController;
	private JTable table;
	
	private ArrayList<ChooseCourseVO> chooseCourseVOs;
	private StudentController studentController; 
	private double gpa;
	private JTextField textField;
	final JComboBox comboBox = new JComboBox();
	/**
	 * Create the panel.
	 */
	public ShowScorePanel(ChooseCourseController chooseCourseController,StudentController studentController) {
		this.chooseCourseController=chooseCourseController;
		this.studentController=studentController;
		try {
			chooseCourseVOs=chooseCourseController.getAllChooseCourseRecord();
		} catch (Exception e1) {
			
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}
		try {
			gpa=studentController.getGPA(ChooseCourse.NOW_TERM);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			ExceptionHandler.networkException();
		
		}
		setSize(800, 600);
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(69, 90, 707, 366);
		this.add(scrollPane);
		
		table = new JTable();
		table.setEnabled(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null,null,null}
			},
			new String[] {
					 "课程编号","课程名","学分","成绩"
			}
		) 
		{
			Class[] columnTypes = new Class[] {
				String.class, String.class,String.class,String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		table.setBounds(119, 424, 262, 143);
		scrollPane.setViewportView(table);

		textField = new JTextField();
		textField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		textField.setBounds(566, 22, 97, 39);
		add(textField);
		textField.setColumns(10);
		textField.setText(gpa+"");
		textField.setEditable(false);
		
		table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		
		
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"大一上", "大一下", "大二上"}));
		comboBox.setBounds(53, 32, 133, 21);
		add(comboBox);
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange()==ItemEvent.SELECTED) {
					int index=comboBox.getSelectedIndex();
					changeList(index);
					
				}
			}
		});
		comboBox.setSelectedIndex(ChooseCourse.NOW_TERM-1);
		
		JLabel lblNewLabel = new JLabel("GPA：\r\n");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		lblNewLabel.setBounds(477, 33, 79, 15);
		add(lblNewLabel);
		

		fillTable();
		
	}
	
	public void fillTable(){
		
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		 tableModel.setRowCount(0);
		 for(ChooseCourseVO chooseCourseVO:chooseCourseVOs){
			 String[] array=new String [4];
			 array[0]=chooseCourseVO.getCourseId();
			 array[1]=chooseCourseVO.getCourseName();
			 array[2]=chooseCourseVO.getCreidt()+"";
			 if (chooseCourseVO.getScore()==0) {
				 array[3]="该课程成绩未出";
			 } 
			 else {
				 array[3]=chooseCourseVO.getScore()+"";
		 	 }
			 
			
			 tableModel.addRow(array);
			 table.invalidate();
			 table.updateUI();
	      }
	}

	public void changeList(int index) {
		System.out.println(index);
		 try {
			gpa = studentController.getGPA(index + 1);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			ExceptionHandler.networkException();
		}
		 System.out.println(gpa);
		 textField.setText(gpa+"");
		 
		textField.repaint();
		try {
			chooseCourseVOs = chooseCourseController.getChooseCourseByTerm(index + 1);
		} catch (Exception e) {
		
			e.printStackTrace();
			ExceptionHandler.networkException();
		}
		fillTable();
	}
	
	public void refresh(){
		comboBox.setSelectedIndex(ChooseCourse.NOW_TERM-1);
		changeList(ChooseCourse.NOW_TERM-1);
	}

	@Override
	protected void paintComponent(Graphics g) {
		fillTable();
	} 
}
